﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MongoDBDemo.Models;
using MongoDBDemo.Models.domain;
using MongoDBDemo.Models.services;

namespace MongoDBDemo.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly StudentService _studentService;
        public HomeController(ILogger<HomeController> logger, StudentService studentService)   //构造函数注入
        {
            _logger = logger;
            _studentService = studentService;
        }

        public IActionResult Index()
        {
            //List<Student> stds = _studentService.Get();

            Student std = new Student()
            {
                Age = 23,
                Name = "xxx",
                IsDelete = false,
                CreateDate = DateTime.Now
            };
            std = _studentService.Create(std);

            return View();
        }

        #region CRUD

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public ActionResult<List<Student>> Get()
        {
            return _studentService.Get();
        }

        /// <summary>
        /// 根据id获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public ActionResult<Student> Get(string id)
        {
            var Student = _studentService.Get(id);
            if (Student == null)
            {
                return NotFound();
            }
            return Student;
        }

        /// <summary>
        ///添加
        /// </summary>
        /// <param name="Student"></param>
        /// <returns></returns>
        public ActionResult<Student> Create(Student Student)
        {
            _studentService.Create(Student);
            return Ok();
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <param name="StudentIn"></param>
        /// <returns></returns>
        public IActionResult Update(string id, Student StudentIn)
        {
            var Student = _studentService.Get(id);

            if (Student == null)
            {
                return NotFound();
            }
            _studentService.Update(id, StudentIn);
            return NoContent();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult Delete(string id)
        {
            var Student = _studentService.Get(id);

            if (Student == null)
            {
                return NotFound();
            }
            _studentService.Remove(Student.Id);
            return NoContent();
        }
        #endregion


        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
